package com.dingding.javabasic.ThreadLocal问题;

import com.google.common.collect.Lists;

import java.util.List;

public class Solution {

    private List<String> messages = Lists.newArrayList();

    //创建ThreadLocal的同时把Solution对象存进去
    public static final ThreadLocal<Solution> holder = ThreadLocal.withInitial(Solution::new);

    public static void add(String message) {
        holder.set(new Solution());
        holder.get().messages.add(message);
    }

    public static List<String> clear() {
        List<String> messages = holder.get().messages;
        holder.remove();

        System.out.println("size: " + holder.get().messages.size());
        return messages;
    }

    public static void main(String[] args) {
        Solution.add("一枝花算不算浪漫");
        List<String> messages1 = holder.get().messages;
        System.gc();
//        Solution.clear();
    }
}
